/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using DaveSexton.DocProject.Resources;
using EnvDTE80;
using Microsoft.VisualStudio.TemplateWizard;

namespace DaveSexton.DocProject.Wizards
{
	[CLSCompliant(false)]
	public sealed class VSWizard : IWizard
	{
		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="VSWizard" /> class.
		/// </summary>
		public VSWizard()
		{
		}
		#endregion

		#region IWizard Members
		public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, 
			object[] customParams)
		{
		}

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
		public void ProjectFinishedGenerating(EnvDTE.Project project)
		{
			if (project == null)
				return;

			// If the add-in has not been intialized (e.g., if the wizard is running in an express edition of Visual Studio), 
			// then make sure that it's initialized before continuing.  The Initialze method will simply exit if it has already executed.
			DocProjectEnvironment.Initialize((DTE2) project.DTE);

			if (!ProjectFactory.IsDocProject(project))
				throw new ArgumentException(Errors.InvalidProjectNotDocProject, "project");

			IDocProject docProject = (IDocProject) ProjectFactory.CreateProject(project);

			EventHandler<Foundation.ExceptionEventArgs> errorHandler = delegate(object sender, Foundation.ExceptionEventArgs e)
			{
				try
				{
					Log.Exception(e.Exception, Resources.Text.NewProjectWizardError);
				}
				finally
				{
					DocProjectEnvironment.ShowErrorDialogWithCaption(
						Resources.Text.WizardErrorTitle, Errors.WizardError, Environment.NewLine, e.Exception.Message);
				}
			};

			try
			{
				DocProjectConfigurationWizard.Run(docProject, errorHandler);
			}
			catch (Exception ex)
			{
				errorHandler(null, new Foundation.ExceptionEventArgs(ex));
			}
		}

		public void BeforeOpeningFile(EnvDTE.ProjectItem projectItem)
		{
		}

		public void ProjectItemFinishedGenerating(EnvDTE.ProjectItem projectItem)
		{
		}

		public void RunFinished()
		{
		}

		public bool ShouldAddProjectItem(string filePath)
		{
			return true;
		}
		#endregion
	}
}
